package com.buyingtickets.controller;

import com.buyingtickets.dao.PassengersDao;
import com.buyingtickets.dao.TrainNumberDao;
import com.buyingtickets.entity.Passenger;
import com.buyingtickets.entity.TrainNumber;
import com.buyingtickets.entity.User;
import com.buyingtickets.service.TicketBiz;
import com.buyingtickets.service.TrainNumberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("order")
public class TrainOrderInfoController {
    @Resource
    private PassengersDao passengersDao;
    @Resource
    private TicketBiz ticketBiz;
    @Resource
    private TrainNumberDao trainNumberDao;
    @Resource
    private TrainNumberService trainNumberService;

    @RequestMapping("{siteStart}/{siteEnd}/{trainNumberId}/{startDate}/trainOrderInfo")
    public String trainOrderInfo(@PathVariable String siteStart,
                                 @PathVariable String siteEnd,
                                 @PathVariable int trainNumberId,
                                 @PathVariable String startDate,Model model, HttpSession httpSession){
        String startDate1=startDate.substring(0,10);
        List<TrainNumber>  trainNumberList= trainNumberService.queryBySiteDao(siteStart,siteEnd,startDate1);
       for(int i=0;i<trainNumberList.size();i++){
           System.out.println("------------------");
           if(trainNumberList.get(i).getTrainNumberId()==trainNumberId){
               model.addAttribute("trainNumber",trainNumberList.get(i));
               httpSession.setAttribute("tofcid",trainNumberList.get(i).getTrain().getTrainId());
               httpSession.setAttribute("trainNumberId",trainNumberId);
               httpSession.setAttribute("siteStart",siteStart);
               httpSession.setAttribute("siteEnd",siteEnd);
               httpSession.setAttribute("startDate",startDate);
               httpSession.setAttribute("trainNumberId",trainNumberId);
           }

       }

        model.addAttribute("siteStart",siteStart);
        model.addAttribute("siteEnd",siteEnd);

        httpSession.setAttribute("tofcid",trainNumberId);
        User user= (User) httpSession.getAttribute("User");
        List<Passenger> passengerList=passengersDao.queryAll(user.getUserId());
        model.addAttribute("plist",passengerList);
        return "trainOrderInfo";
    }
    @RequestMapping("{id}/exchangeTrainOrderInfo")
    public String exchangeTrainOrderInfo(@PathVariable int id, Model model, HttpSession httpSession){
        httpSession.setAttribute("tofcid",id);
        User user= (User) httpSession.getAttribute("User");
        List<Passenger> passengerList=passengersDao.queryAll(user.getUserId());
        model.addAttribute("plist",passengerList);
        return "exchangeTrainOrderInfo";
    }

}
